package com.mall.admin.controller.goods;

import java.util.List;

import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.Kv;
import com.mall.admin.controller.AdminBaseController;
import com.mall.admin.service.goods.GoodsCategoryService;
import com.mall.admin.service.goods.GoodsInfoService;
import com.mall.common.annotation.Inject;
import com.mall.common.model.goods.GoodsCategory;
import com.mall.common.model.goods.GoodsInfo;
import com.mall.common.service.base.Result;
import com.mall.common.util.ResultCode;

/**
 * 货品信息接口
 * @author taoge
 * @version 1.0
 * @create_at 2017年8月27日下午12:04:00
 */
@ControllerBind(controllerKey="/admin/goods/info")
public class GoodsInfoController extends AdminBaseController {

	@Inject
	private GoodsInfoService goodsInfoService;
	
	@Inject
	private GoodsCategoryService goodsCategoryService;
	
	
	public void index(){
		renderView("/goods/info/index.vm");
	}
	
	/**
	 * 商品列表
	 */
    public void list(){
		int goodsId = getParaToInt(0,0);
		int pageNumber = getParaToInt("pageNumber",1);
		int pageSize = getParaToInt("pageSize",10);
		Kv condition = Kv.by("goodsId", goodsId).set("pageNumber",pageNumber)
		                 .set("pageSize",pageSize);
		Result result = goodsInfoService.list(condition);
		setAttr("page", result.getDefaultModel());
		renderView("/goods/info/list.vm");
	}
    
    /**
	 * 添加商品
	 */
	public void add(){
		Result result = goodsInfoService.addGoods();
		List<GoodsCategory> categoryList = goodsCategoryService.getCategoryByParent();
		setAttr("categoryList", categoryList);
		setAttr("brands", result.getModel("brands"));
		setAttr("units", result.getModel("units"));
		setAttr("propertys", result.getModel("propertys"));
		renderView("/goods/info/add.vm");
	}
	
	/**
	 * 图片上传
	 */
	public void uploadimage(){
		String path = uploadRename(getFile("file"));
		renderJson(new ResultCode(ResultCode.SUCCESS, path));
	}
	
	/**
	 * 保存商品
	 */
	public void create(){
    	GoodsInfo goodsInfo = getModel(GoodsInfo.class, "goodsInfo");
    	String propertyId = getPara("propertyIds");
    	String[] groupGoods = getParaValues("group_goods");
    	String[] salePrices = getParaValues("sale_price");
    	String[] stocks = getParaValues("stock");
    	String[] images = getParaValues("image");
    	Kv params = Kv.by("groupGoods", groupGoods).set("propertyId", propertyId)
    			      .set("salePrices", salePrices).set("stocks", stocks).set("images", images);
    	Result result = goodsInfoService.create(goodsInfo, params);
    	renderJson(result.getResultCode());
	}

    /**
     * 商品编辑页
     */
    public void edit(){
		int goodsId = getParaToInt(0);
		setAttr("goodsInfo", GoodsInfo.dao.findById(goodsId));
		renderView("/goods/info/edit.vm");
	}
    
    /**
     * 更新商品
     */
    public void update(){
		
	}
   
}
